package com.ruoyi.common.utils.digest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.io.IOException;
import java.util.Map;
import java.util.regex.Pattern;

public class Sign {
    public static void main(String[] args) throws IOException {
//            String str = "{\n" +
//                    " \"pageSize\":50,\n" +
//                    " \"pageNo\":1,\n" +
//                    " \"facilitatorCode\":\"MTQ1Njc4OTAxMjM0NTY3OD\",\n" +
//                    " \"carNumber\":\"粤A11111\",\n" +
//                    " \"catType\":1\n" +
//                    "}";

        String str = "{\n" +
                " \"username\":\"j6nSKF6bT2dww7Y25h+JYHGz4CJEEzBPyvxlFu5C/ZMzMw7SXRNQT6pmD86xB/YqAU97fV+WouCGtS4Tf3b6weYHXBYSu6WSgu8MeWW6ui4NekXsTYlTcEWkVplMZSdRJeJysT6Squ3t18fwYQTBcAKVB07MGGyUufYP6xE5hMI=\",\n" +
                " \"password\":\"SsQ+HCMt1P7c3bi/1vElVlZTfV9HQvfqNIAZENhXAnxwj+FNcwaGKU9+XtTbsoNvZp3B+7JO0tVCSjsnIiXGDlgrXw5vnhl0rQpj3ecbCRTp0wTkP037CoVYqt5OZTpcjL70LfCkEnWN2Ax4UlSw1LoxcR/idXwLHolhHoYbQGs=\"\n" +
                "}";
        //清除换行符
        str = Pattern.compile("[\t\r\n]").matcher(str).replaceAll("");

        //jaskson解析
        ObjectMapper OBJECT_MAPPER = new ObjectMapper();

        //转成map
        Map jsonNode = OBJECT_MAPPER.readValue(str, Map.class);

        //排序
        OBJECT_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        //生成json
        String string = OBJECT_MAPPER.writeValueAsString(jsonNode);
        //签名密钥
        String signKey = "testKey";

        //HmacSHA265 加密
        String encode = DigestUtil.hmacDegist(DigestUtil.HmacSHA256, signKey.getBytes(), string).toBase64();
        System.out.println(encode);
    }
}


